"use strict";

/**
 * @ngdoc directive
 * @name jgappApp.directive:dateDirective
 * @description
 * # dateDirective
 */
angular.module("jgappApp").directive("dateDirective", [
  "$filter",
  function($filter) {
    return {
      require: "ngModel",
      scope: {
        model: "=ngModel"
      },
      restrict: "AE",
      link: function(scope, element, attrs, ngModelController) {
        var type;
        type = attrs.datetype || "date";
        // 设置只读防止键盘弹起
        element[0].setAttribute("readOnly",'true')
        element.bind("click", function() {
          NativeUtil.use("Datepicker", function(data) {
            console.log("date", data);
            ngModelController.$setViewValue(data);
            element[0].value=data;
            scope.$apply();
          },type);
        });
        ngModelController.$formatters.push(function(data) {
          //convert data from model format to view format
          var date;
          if (type == "datetime") {
            date = $filter("dateFmt")(data, "yyyy-MM-dd hh:mm:ss");
          } else if (type == "time") {
            // date = $filter("dateFmt")(data, "hh:mm")
            date = data;
          } else {
            date = $filter("dateFmt")(data);
          }
          return date; //converted
        });
      }
    };
  }
]);
